package com.example.sixthweek;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.AlteredCharSequence;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateListActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener{
    private static final String TAG = "test";
    private ProgressBar bar;
    Handler handler;
    ListView myList;
    MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list2);
        bar = findViewById(R.id.nodata);
//        myList.setEmptyView(bar);

        ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("cname", "Rate:" + i);
            map.put("cval", "detail:" + i);
            listItems.add(map);
        }

        ListAdapter ListItemAdapter = new SimpleAdapter(this, listItems,R.layout.list_item,new String[]{"cname","cval"},new int[]{R.id.tv7,R.id.tv8});
        myList = findViewById(R.id.mylist);
        myList.setAdapter(ListItemAdapter);
        myList.setOnItemClickListener(this);
        myList.setOnItemLongClickListener(this);
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {

                //收到数据
                if(msg.what==5){
                    ArrayList<HashMap<String, String>> rateList = (ArrayList<HashMap<String, String>> )msg.obj;
//                    ListAdapter listItemAdapter = new SimpleAdapter(RateListActivity2.this, rateList, R.layout.list_item, new String[]{"cname","cval"}, new int[]{R.id.tv7,R.id.tv8});
//                    ListView myList1 = findViewById(R.id.mylist);
                    myAdapter = new MyAdapter(RateListActivity2.this,R.layout.list_item,rateList);
                    myList.setAdapter(myAdapter);
                    bar.setVisibility(View.GONE);

                }
                super.handleMessage(msg);
            }
        };
        myList.setOnItemLongClickListener(this);
        MapTask mapTask = new MapTask();
        mapTask.setHandler(handler);

        Thread t = new Thread(mapTask);
        t.start();

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        HashMap<String,String> rowData = (HashMap<String,String>)myList.getItemAtPosition(position);
        String title = rowData.get("cname");
        String detail = rowData.get("cval");

        Log.i(TAG, "onItemClick: "+title);
        Log.i(TAG, "onItemClick: "+detail);

        Intent intent = new Intent(this,CountRate.class);
        intent.putExtra("titleKey",title);
        intent.putExtra("detailKey",detail);
        startActivity(intent);
    }


    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
        Log.i(TAG, "onItemLongClick: pos="+ position);
        //删除数据处理
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示")
                .setMessage("是否删除当前数据")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        myAdapter.remove(myList.getItemAtPosition(position));

                    }
                }).setNegativeButton("否",null);
        builder.create().show();

        return true;//true表示不会再识别单击的效果
    }
}